package api

import (
	"elastalert2-webhook/service" // 替换elastalert2-webhook为你的模块名
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func HandleWebhook(c *gin.Context) {
	alertData, err := service.ProcessRequest(c)
	if err != nil {
		logrus.Errorf("Error processing request: %v", err)
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	if err := service.SendAlert(alertData); err != nil {
		logrus.Errorf("Error sending alert: %v", err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Webhook sent successfully"})
}
